/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logging;

import AccessLayer.AccessLayer;
import java.text.SimpleDateFormat;
import java.util.Properties;

/**
 *
 * @author Marco
 */
public class Logger {

    private static boolean on = true;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat(
            "dd.MM.yyyy HH:mm:ss");
    private static LogLevel.Level globalLevel;
    private static LogLevel.Level guiLog;


    public static void configure(){
        Properties p = AccessLayer.giveConfig();
        on = Boolean.valueOf(p.getProperty("Debug", "false"));
        globalLevel = LogLevel.Level.valueOf(p.getProperty("DebugLevel", "OFF"));
        guiLog = LogLevel.Level.valueOf(p.getProperty("GuiLogLevel", "USER"));

    }

    /**
     * Return true if logging is on.
     */
    public static boolean isOn() {
        return on;
    }

    /**
     * Set logging on/off.
     */
    public static void setOn(boolean isOn) {
        on = isOn;
    }

    /**
     * Log the given message.
     */
    public static void info(String msg) {
        if (isOn()&& globalLevel.compareTo(LogLevel.Level.INFO) <= 0) {
            AppendManager.write(LogLevel.Level.INFO, msg);
        }

    }

    public static void user(String msg){
        if (isOn() && guiLog.compareTo(LogLevel.Level.USER) <= 0){
            AppendManager.write(guiLog, msg);
        }
    }

    public static void trace(String msg) {
        if (isOn() && globalLevel.compareTo(LogLevel.Level.TRACE) <= 0) {
            AppendManager.write(LogLevel.Level.TRACE, msg);
        }
    }

    public static void debug(String msg) {
        if (isOn() && globalLevel.compareTo(LogLevel.Level.DEBUG) <= 0) {
            AppendManager.write(LogLevel.Level.DEBUG, msg);
        }
    }

    public static void warn(String msg) {
        if (isOn() && globalLevel.compareTo(LogLevel.Level.WARN) <= 0) {
            AppendManager.write(LogLevel.Level.WARN, msg);
        }
    }

    public static void error(String msg, Exception ex) {
        if (isOn() && globalLevel.compareTo(LogLevel.Level.ERROR) <= 0) {
            AppendManager.write(LogLevel.Level.ERROR, msg, ex);
        }
    }

    public static void fatal(String msg, Exception ex) {
        if (isOn() && globalLevel.compareTo(LogLevel.Level.FATAL) <= 0) {
            AppendManager.write(LogLevel.Level.FATAL, msg, ex);
        }
    }

    public static void main(String[] args) {
        Logger.configure();
        AppendManager.configure();
        Logger.trace("Test started.");
        Logger.info("Logging disabled after this.");
       Logger.setOn(false);
        Logger.fatal("This message should not be in the log.", new Exception());
        Logger.setOn(true);
        Logger.debug("Logging enabled.");
        Logger.warn("Test completed.");
    }

    /**
     * @return the dateFormat
     */
    public static SimpleDateFormat getDateFormat() {
        return dateFormat;
    }
}
